//
//  AppDelegate.m
//  map
//
//  Created by 000 on 16/7/22.
//  Copyright © 2016年 faner. All rights reserved.
//

#import "AppDelegate.h"
#import "QZNetworkTool.h"
//#import "MapVC.h"
#import "QZLoginVC.h"
#import <BmobSDK/Bmob.h>
#import "constant.h"
#import "QZManagerVC.h"
#import "QZViewController.h"


#define appKey @"9ce75fe364dca41cab990b4e6951fe97"
//#define appSecret @"b1e2e034a6154b0ba461f5de73068233"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //初始化应用，appKey和appSecret从后台申请得
    [Bmob registerWithAppKey:appKey];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self initControllers];
    [self.window makeKeyAndVisible];
    NSLog(@"%@",[UIApplication sharedApplication].windows);

    return YES;
}

- (void)initControllers
{
    //添加观察者
    //当网络发生改变的时候Reachability会发送    kReachabilityChangedNotification这个通知 通知观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeState:) name:kReachabilityChangedNotification object:nil];
    
    self.reach = [Reachability reachabilityForInternetConnection];
    [self.reach startNotifier];//开始观察分析 这个网址的网络状态
    [self loadCache];
}

- (void)loadCache
{
    
    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
    if ([[userdefaults objectForKey:@"detailSave"] isEqualToString:@"用户未登录"] || [userdefaults objectForKey:@"detailSave"] == nil) {
        
        QZLoginVC *loginVC = [[QZLoginVC alloc] init];
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginVC];
         NSLog(@"%@",self.window);
        
    } else if ([[userdefaults objectForKey:@"detailSave"] isEqualToString:@"18210637213"]){
        
        QZManagerVC *managerVC = [[QZManagerVC alloc] init];
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:managerVC];
    }
    else {

        QZViewController *userVC = [[QZViewController alloc] init];
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:userVC];
        }
    
}
- (void)logOut
{
    QZLoginVC *loginVC = [[QZLoginVC alloc] init];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginVC];
}
- (void)changeState:(NSNotification *)noti
{
    Reachability *reach = noti.object;
    
//    NSLog(@"网络状态改变");
    if (reach.currentReachabilityStatus == ReachableViaWiFi) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"网络WIFI" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
//        NSLog(@"Notification Says Unreachable");
//        NSLog(@"切换到wifi");
    } else if (reach.currentReachabilityStatus == ReachableViaWWAN){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"网络WWAN" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
//        NSLog(@"Notification Says Unreachable");
//        NSLog(@"切换到手机网络");
    } else if (reach.currentReachabilityStatus == NotReachable) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"网络已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
//        NSLog(@"Notification Says Unreachable");
//        NSLog(@"切换到无网状态");
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
